﻿using System;
using System.Web.Mvc;
using ProjetoComperio.BusinessLogicLayer;
using ProjetoComperio.DataLayer.Entities;
using ProjetoComperio.Framework.Web.Security.UrlEncrypt;
using ProjetoComperio.SystemFramework.Web;

namespace ProjetoComperio.Presentation.Web.Controllers
{
    public class CommentController : ComperioController
    {
        private SubjectBLL _subjectBLL;
        public SubjectBLL SubjectBLL
        {
            get
            {
                if (_subjectBLL == null)
                    _subjectBLL = new SubjectBLL();

                return _subjectBLL;
            }

        }

        private CommentBLL _commentBLL;
        public CommentBLL CommentBLL
        {
            get
            {
                if (_commentBLL == null)
                    _commentBLL = new CommentBLL();

                return _commentBLL;
            }
        }

        public ActionResult Index()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Create(String id)
        {
            Session["id"] = URLEncrypt.DecryptFromString(id);

            var subject = SubjectBLL.RetrieveSubject(Convert.ToInt32(Session["id"]));

            //Temp
            if (Session["UrlReferrer"] == null)
                Session["UrlReferrer"] = Request.UrlReferrer;

            ViewData["SubjectTitle"] = subject.Title;
            ViewData["SubjectMessage"] = subject.Message;

            ViewData["SubjectCreatedDate"] = subject.CreatedDate.ToString("dd/MM/yyyy HH:mm");
            ViewData["SubjectUserName"] = subject.UserName;

            ViewData["Comments"] = CommentBLL.RetrieveComments(Convert.ToInt32(Session["id"]));
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(Comment comment)
        {
            comment.CreatedDate = DateTime.Now;
            comment.SubjectId = Convert.ToInt32(Session["id"]);
            comment.Email = CurrentUser.Email;
            comment.UserName = CurrentUser.UserName;

            CommentBLL.Create(comment);

            return Create(URLEncrypt.EncryptToString(comment.SubjectId.ToString()));
        }
    }
}
